package com.heifeng.cloudshelf.base;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;


import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.utils.IntentUtil;
import com.heifeng.cloudshelf.widget.BlackStyleBackTitleBar;

import butterknife.BindView;

/**
 * 这是网页
 */
public abstract class BaseWebWiewActivity extends SimpleActivity {

    @BindView(R.id.top_toolbar)
    public BlackStyleBackTitleBar topToolbar;
    @BindView(R.id.web_view)
    WebView webView;
    @BindView(R.id.progress)
    ProgressBar progressBar;

    protected WebSettings webSettings;

    public static void open(Context context) {
        IntentUtil.startActivity(context, BaseWebWiewActivity.class);
    }

    public abstract String getToolbarTitle();

    public abstract String getUrl();

    @Override
    protected int getLayout() {
        return R.layout.activity_web_wiew;
    }

    @Override
    protected void initEventAndData() {
        initWebView();
    }

    public void initWebView() {
        showProgressDialog("请求中...");
        topToolbar.setTitle(getToolbarTitle());
        webView.loadUrl(getUrl());
        Log.i(TAG, "initWebView: " + getUrl());

        webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);//支持js
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);//设置允许JS弹窗
        webSettings.setAllowFileAccess(true);//设置可以访问文件
        webSettings.supportMultipleWindows();//多窗口
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
        //webview的缓存模式
        webSettings.setAppCacheEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setDomStorageEnabled(true);
        webSettings.setNeedInitialFocus(true);

        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true);//将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true);// 缩放至屏幕的大小

        webSettings.setSupportZoom(true);  //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true);//设置内置的缩放控件。
        webSettings.setDisplayZoomControls(false);//隐藏原生的缩放控件

        //复写shouldOverrideUrlLoading()方法，使得打开网页时不调用系统浏览器， 而是在本WebView中显示
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url != null) view.loadUrl(url);
                return true;
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
//                    progressBar.setVisibility(View.GONE);
                    dimissProgressDialog();
                } else {
//                    progressBar.setVisibility(View.VISIBLE);
//                    progressBar.setProgress(newProgress);

                }
            }
        });

        webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //http/https混合加载
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        if (webView != null) {
            webView.removeAllViews();
            webView.destroy();
        }
        super.onDestroy();
    }

}
